package com.ruoyi.web.controller.base;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.live.base.domain.LiveProduct;
import com.ruoyi.live.base.domain.LiveProductItem;
import com.ruoyi.live.base.service.ILiveProductService;
import com.ruoyi.live.configuration.SessionInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import jakarta.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
 * 商品管理
 */
@RestController
@RequestMapping({"/base/product"})
public class LiveProductController extends BaseController {
    @Autowired
    ILiveProductService liveProductService;

    @GetMapping("/list")
    public TableDataInfo list(LiveProduct entity) {
        this.startPage();
        QueryWrapper<LiveProduct> liveProductItemQueryWrapper = new QueryWrapper<>(entity);
        List<LiveProduct> list = liveProductService.list(liveProductItemQueryWrapper);
        return this.getDataTable(list);
    }

    @PreAuthorize("@ss.hasAnyRoles('admin')")
    @Log(title = "商品信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, LiveProduct liveProduct)
    {
        QueryWrapper<LiveProduct> liveProductItemQueryWrapper = new QueryWrapper<>(liveProduct);
        List<LiveProduct> list = liveProductService.list(liveProductItemQueryWrapper);
        ExcelUtil<LiveProduct> util = new ExcelUtil<LiveProduct>(LiveProduct.class);
        util.exportExcel(response, list, "商品数据");
    }

    /**
     * 根据商品编号获取详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(liveProductService.getById(id));
    }

    @PreAuthorize("@ss.hasAnyRoles('admin')")
    @Log(title = "商品管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody LiveProduct entity) {
        entity.setCreateTime(System.currentTimeMillis());
        entity.setCreateUser(SessionInfo.getUserId());
        entity.setState(1);
        return toAjax(liveProductService.save(entity));
    }

    @PreAuthorize("@ss.hasAnyRoles('admin')")
    @Log(title = "商品管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(LiveProduct entity) {
        return toAjax(liveProductService.updateById(entity));
    }

    @PreAuthorize("@ss.hasAnyRoles('admin')")
    @Log(title = "商品管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult delete(@PathVariable Long[] ids) {
        List<Long> list = Arrays.asList(ids);
        return toAjax(liveProductService.removeByIds(list));
    }
}
